package com.kanshu.ksgb.fastread.doudou.module.login.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.base.baseadapter.CustomFragmentAdapter;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity;
import com.kanshu.ksgb.fastread.doudou.common.business.event.BindEvent;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.StorageUtils;
import com.kanshu.ksgb.fastread.doudou.common.view.tablayout.SlidingTabLayout;
import com.kanshu.ksgb.fastread.doudou.module.login.fragment.ChargeHistoryFragment;
import com.kanshu.ksgb.fastread.doudou.module.login.fragment.ConsumeHistoryFragment;
import com.kanshu.ksgb.fastread.doudou.module.personal.activity.ChargeFragment;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.ThirdBindInfo;
import com.kanshu.ksgb.fastread.doudou.module.personal.presenter.AccountPresenter;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wcy on 2017\6\22 0022.
 */
@Route(path = ARouterConfig.LOGIN_MY_ACCOUNT)
public class MyAccountActivity extends BaseActivity implements IGenrialMvpView<BaseResult<ThirdBindInfo>> {
/*
    @BindView(R.id.we_chat)
    TextView mWeChat;
    @BindView(R.id.qq)
    TextView mQQ;
    @BindView(R.id.third_account)
    LinearLayout mThirdAccount;
    @BindView(R.id.sina)
    TextView mSina;*/
    @BindView(R.id.tab_layout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.pager)
    ViewPager mPager;
    AccountPresenter mPresenter = new AccountPresenter(lifeCyclerSubject);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_layout);
        ButterKnife.bind(this);
        init();
        setTitle("金币充值");
    }

    private void init() {
        setTitle("账户");
        mPresenter.setThirdBindView(this);
        List<Fragment> list = new ArrayList<>();
        list.add(ChargeFragment.newInstance(ChargeFragment.TYPE_CHARGE));
        list.add(new ChargeHistoryFragment());
        list.add(new ConsumeHistoryFragment());
        String[] strs = new String[]{"充值", "充值记录", "消费记录"};
        CustomFragmentAdapter pagerAdapter = new CustomFragmentAdapter(getSupportFragmentManager(), list);
        mPager.setAdapter(pagerAdapter);
        mTabLayout.setViewPager(mPager, strs);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 99)
    public void handleBindEvent(BindEvent event) {
        if (event.code == BindEvent.SUCCESS) {
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        mPresenter.detachView();
    }

    public static void actionStart(Context context) {
        ARouterUtils.toActivity(ARouterConfig.LOGIN_MY_ACCOUNT);
    }

    @Override
    public void showContent(BaseResult<ThirdBindInfo> info) {
        if (BaseResult.isNotNull(info)) {
            ThirdBindInfo data = info.result.data;
            if (data != null) {
                StorageUtils.setPreference(this, Constants.SP_NAME, Constants.WX_LOGIN,
                        data.is_weixin_bind == ThirdBindInfo.BIND_SUCCESS);
                StorageUtils.setPreference(this, Constants.SP_NAME, Constants.QQ_LOGIN,
                        data.is_qq_bind == ThirdBindInfo.BIND_SUCCESS);
                StorageUtils.setPreference(this, Constants.SP_NAME, Constants.SINA_LOGIN,
                        data.is_weibo_bind == ThirdBindInfo.BIND_SUCCESS);
                if (StorageUtils.getPreference(this, Constants.SP_NAME, Constants.WX_LOGIN,
                        false) || StorageUtils.getPreference(this, Constants.SP_NAME, Constants.QQ_LOGIN,
                        false) || StorageUtils.getPreference(this, Constants.SP_NAME, Constants.SINA_LOGIN,
                        false)) {
                }
            }
        }
    }

    @Override
    public void showError(int errCode, String errDesc) {

    }

    @Override
    public void showLoading(String s) {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }
}
